import { PropType, h, defineComponent, VNodeArrayChildren } from "vue";
import {
    isSideBarGroup,
    isSideBarLink,
    SideBarGroup,
    SideBarItem,
} from "./catalogueconfig";
import { getlinkhrefmd } from "./getlinkhrefmd";
import { usemarkdownurl } from "./usemarkdownurl";
export const SideBarLink = defineComponent({
    props: {
        item: {
            type: Object as PropType<SideBarItem>,
            required: true,
        },
    },
    setup(props) {
        const routemd = usemarkdownurl();
        return () => {
            const { item } = props;

            if (isSideBarGroup(item)) {
                const childItems = createChildren(item);
                return h("li", { class: "sidebar-link" }, childItems);
            }
            if (isSideBarLink(item)) {
                const active = item.link === routemd.value;
                const isextralink = !item.link.endsWith(".md");
                return h(
                    "li",
                    { class: "sidebar-link" },
                    h(
                        "a",
                        {
                            rel: isextralink
                                ? "noopener noreferrer"
                                : undefined,
                            target: isextralink ? "_blank" : "_self",
                            href: isextralink
                                ? item.link
                                : getlinkhrefmd(item.link),
                            class: [
                                "sidebar-link-item",
                                "mui-btn mui-btn-primary mui-btn-outlined mybutton-8731e6c5bb5148e49e14cca7cdfa73e8  ",
                                isextralink ? undefined : "navigate",
                                active ? "active" : undefined,
                            ]
                                .filter(Boolean)
                                .join(" "),
                        },
                        item.text
                    )
                );
            }
            return;
        };
    },
});
function createChildren(item: SideBarGroup): VNodeArrayChildren {
    return [
        h(
            "details",
            { open: true },
            [
                h(
                    "summary",
                    { class: "sidebar-link-item", style: "padding-left: 0px;" },
                    [item.text]
                ),
                item.children.length > 0
                    ? h("ul", { class: "sidebar-links" }, [
                          ...item.children.map((ite, index) => {
                              return h(SideBarLink, { item: ite, key: index });
                          }),
                      ])
                    : null,
            ].filter(Boolean)
        ),
    ];
}
